

package com.sunny.gateway.handler;


import com.alibaba.fastjson.JSON;
import com.sunny.kratos.common.core.util.R;
import com.sunny.gateway.dto.Menu;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

/**
 * @author sunny
 * @date 2018/7/5 验证码生成逻辑处理类
 */
@Slf4j
@Component
public class ReportHandler implements HandlerFunction<ServerResponse> {

	@Autowired
	private  RedisTemplate<String, Object> redisTemplate;

	@Override
	public Mono<ServerResponse> handle(ServerRequest serverRequest) {
		return serverRequest.bodyToMono(String.class)
				.flatMap(userRequest -> {
					Menu menu = JSON.parseObject(userRequest,Menu.class);

					redisTemplate.opsForValue().set("gateway:menu:"+menu.getServiceName(),menu);// 返回响应
					return ServerResponse.ok().bodyValue(BodyInserters.fromValue(R.ok()));
				}).onErrorResume(throwable -> {
					log.error("上报菜单出现异常",throwable);
					return ServerResponse.ok().bodyValue(BodyInserters.fromValue(R.failed()));
				});
	}


}
